package demo.practice.leetcode_eveyday;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

public class P264 {


   /* PriorityQueue<Long> queue = new PriorityQueue<Long>((i, i2) -> {
        return i.compareTo(i2);
    });*/


    public static void main(String[] args) {
        P264 p264=new P264();
        p264.nthUglyNumber(10);
    }

    public int nthUglyNumber(int n) {

        //PriorityQueue<Integer> queue = new PriorityQueue<Integer>((i, i2) -> {   return i.compareTo(i2);});

        if(n==1)
            return 1;

        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
        PriorityQueue<Integer> queue2 = new PriorityQueue<Integer>( (i,i2)->i2-i);
        Set<Integer> set = new HashSet<Integer>();
        queue.add(1);
        queue2.add(1);
        set.add(1);
        int[]seeds=new int[]{2,3,4,5};
        for(int i=0;i<n;i++){
            Integer top = queue.poll();
            for(int s:seeds){
                if(set.add(s*top)){
                    queue2.add(s*top);
                    queue.add(s*top);
                    if(queue2.size()==n)
                        return queue2.poll();
                }
            }
        }

        return -1;
    }
}
